<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
        <el-form-item label="考生号" prop="number">
          <el-input v-model="dataForm.number" placeholder="考生号"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="nickname">
        <el-input v-model="dataForm.nickname" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="dataForm.idCard" placeholder="身份证号"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
      </el-form-item>
      <el-form-item label="专业名称" prop="skillGrade">
        <el-input v-model="dataForm.major" placeholder="专业名称"></el-input>
      </el-form-item>
        <el-form-item label="院系" prop="courtyard">
          <el-input v-model="dataForm.courtyard" placeholder="院系"></el-input>
        </el-form-item>
        <el-form-item label="邮政编码" prop="specialty">
          <el-input v-model="dataForm.specialty" placeholder="邮政编码"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
        </el-form-item>
        <el-form-item label="家庭地址" prop="address">
          <el-input v-model="dataForm.address" placeholder="家庭地址"></el-input>
        </el-form-item>
        <el-form-item label="收件人" prop="recipients">
          <el-input v-model="dataForm.recipients" placeholder="收件人"></el-input>
        </el-form-item>
        <el-form-item label="省份" prop="expressNumber">
          <el-input v-model="dataForm.province" placeholder="省份"></el-input>
        </el-form-item>
        <el-form-item label="快递号" prop="expressNumber">
          <el-input v-model="dataForm.expressNumber" placeholder="快递号"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
  </el-dialog>
</template>

<script>
import {informationGetById, informationSave, informationUpdate} from "@/request/utils/information";

  export default {
    data () {
      return {
        visible: false,
        informationId: 0,
        dataForm: {
          nickname: '',
          idCard: '',
          cultureGrade: '',
          skillGrade:'',
          totalPoints: '',
        },
        dataRule: {
          number: [
            { required: true, message: '考生号不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          major: [
            { required: true, message: '专业名称不能为空', trigger: 'blur' }
          ],
          courtyard: [
            { required: true, message: '院系不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' }
          ],
          specialty: [
            { required: true, message: '邮政编码不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '家庭地址不能为空', trigger: 'blur' }
          ],
          recipients: [
            { required: true, message: '收件人不能为空', trigger: 'blur' }
          ],
          expressNumber: [
            { required: true, message: '快递号不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ]
        }
      }
    },

    methods: {
      init (id) {
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            informationGetById(this.dataForm.id).then(data => {
              if (data && data.code === 0) {
                this.dataForm = data.data
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if(this.dataForm.id===undefined){
              informationSave(this.dataForm).then(data => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                  this.dataForm = {}
                } else {
                  this.$message.error(data.msg)
                }
              })
            }else {
              informationUpdate(this.dataForm).then(data => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                  this.dataForm = {}
                } else {
                  this.$message.error(data.msg)
                }
              })
            }

          }
        })
      }
    }
  }
</script>
